package algorithms.leecode;

import java.util.*;

public class Au49 {
    public static void main(String[] args) {
 //       {"martinets","eisenstein"}
        String[] ww = {"eat","tea","tan","ate","nat","bat"};
        List<List<String>> res = groupAnagrams(ww);
        System.err.println(res);
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> resultList = new ArrayList();
        Map<String,Integer> set = new HashMap<>();
        int index1 = 0;
        for (int j = 0; j < strs.length; j++) {
            char[] cs = strs[j].toCharArray();
            Arrays.sort(cs);
            String sc = String.valueOf(cs);
            if (set.containsKey(sc)) {
                int index = set.get(sc);
                resultList.get(index).add(strs[j]);
            } else {
                List<String> list = new ArrayList<>();
                list.add(strs[j]);
                set.put(sc,index1);
                resultList.add(list);
                index1++;

            }
        }
        return resultList;
    }
}
